/*
 * Copyright (C) 2003, 2004 Rex Ford
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*Class GameDisplay.java
 *the actual container for the GamePlay
 */ 
package GamePlay;
import GamePlay.GameObjects.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.ArrayList;

public class GameDisplay extends Container
{	
	private Graphics g;
	private BufferStrategy strategy;
	private GameEnv ge;
	private Frame theFrame;
	private UserShip theUser;
	private int numLayers;
	public Color outerspaceColor=new Color(15,35,60);
	private long lastShot=System.currentTimeMillis();
	
	public GameDisplay(Frame f,final GameEnv theGE,final UserShip user) 
	{
		theUser = user;
		ge = theGE;
		theFrame = f;
		theFrame.createBufferStrategy(2);
		strategy = f.getBufferStrategy();
		g = strategy.getDrawGraphics();
		numLayers = ge.getNumLayers();

		//GET KEYS TO WORK!
		this.setFocusable(true);
		addKeyListener(new KeyListener() 
		{
			public void keyPressed(KeyEvent e) 
			{
				if (e.getKeyChar() == 'p')
				{
					if(ge.paused==false)
					{
						ge.setPaused(true);
					}
					else if(ge.paused==true)
					{
						
						ge.setPaused(false);
					}
				}
				
				if(e.getKeyCode()==KeyEvent.VK_LEFT){user.left=true;}
				if(e.getKeyCode()==KeyEvent.VK_RIGHT){user.right=true;}
				if(e.getKeyCode()==KeyEvent.VK_UP){user.up=true;}
				if(e.getKeyCode()==KeyEvent.VK_DOWN){user.down=true;}
				
				if(e.getKeyCode()==KeyEvent.VK_F)
				{
					if((System.currentTimeMillis()-lastShot)>100)
					{
						lastShot=System.currentTimeMillis();
						user.mustShoot=true;
					}
				}
			}
			
			public void keyTyped(KeyEvent e) 
			{
			};
			
			public void keyReleased(KeyEvent e) 
			{
				if(e.getKeyCode()==KeyEvent.VK_LEFT){user.left=false;}
				if(e.getKeyCode()==KeyEvent.VK_UP){user.up=false;}
				if(e.getKeyCode()==KeyEvent.VK_RIGHT){user.right=false;}
				if(e.getKeyCode()==KeyEvent.VK_DOWN){user.down=false;}
			};
		});
		this.setIgnoreRepaint(true);
	}
	
	
	public void drawBG()
	{
		g.setColor(outerspaceColor);
		g.fillRect(0,0,this.getWidth(),this.getHeight()+30);
	}
	
	public void clearOldSpace()
	{
		g.setColor(outerspaceColor);
		for (int l = 0; l < numLayers; l++) 
		{
			ArrayList GameObjects = ge.getGOs(l);
			for (int x = 0; x < GameObjects.size(); x++) 
			{
				GameObject currentObj = ((GameObject)GameObjects.get(x));
				Image img =currentObj.getImage();
				double xPos = currentObj.locate().getX();
				double yPos = currentObj.locate().getY();
				g.fillRect((int)(0.5+Math.max(0,xPos)),(int)(0.5+Math.max(0,yPos)),img.getWidth(this),img.getHeight(this));
			}
		}
	}
	
	public void pulse(long time) 
	{
		//I hate this...
		this.requestFocus();
		
		for (int l = 0; l < numLayers; l++) 
		{
			ArrayList GameObjects = ge.getGOs(l);
			for (int x = 0; x < GameObjects.size(); x++) 
			{
				GameObject currentObj = ((GameObject)GameObjects.get(x));
				if(currentObj.locate().isInView()==false)
					currentObj.removeMe();
			}
		}
		
		
		for (int l = 0; l < numLayers; l++) 
		{
			ArrayList GameObjects = ge.getGOs(l);
			for (int x = 0; x < GameObjects.size(); x++) 
			{
				GameObject currentObj = ((GameObject)GameObjects.get(x));
				Image img =currentObj.getImage();
				int xPos = (int)(0.5+currentObj.locate().getX());
				int yPos = (int)(0.5+currentObj.locate().getY());
				g.drawImage(img, xPos, yPos, null);
			}
		}
		//hud time!
		drawHud(g);
		strategy.show();
	}
	
	public void drawHud(Graphics g)
	{
		int w=this.getWidth();
		int h=this.getHeight();
		
		//draw bg for this
		g.setColor(new Color(40,50,60));
		g.fillRect(w-300,h-55,w-10,h+5);
		//draw components
		g.setFont(new Font("Arial",0,16));
		g.setColor(Color.WHITE);
		g.drawString("Health: "+theUser.health+"%",w-290,h-35);
		g.drawString("Weapon: "+theUser.numGuns+" Magnum(s)", w-290, h+10);
		
		g.drawString("Score: "+theUser.score,w-100, h-35);
		g.drawString("Lives Left: " +theUser.numLives,w-100,h+10);
		
		g.setColor(new Color(30,35,110));//make this color based on health!
		
		int arcx=w-275;
		int arcy=h-33;
		g.fillArc(arcx,arcy,54,54,180,-180);
		g.setColor(new Color(70,237,130));
		g.fillArc(arcx,arcy,54,54,180,-(int)(((double)theUser.health/100)*180.0));
		g.setColor(new Color(240,240,255));
		g.drawArc(arcx,arcy,54,54,180,-180);
		
	}
}